﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using CoyoEden;
using Controls;
using System.Collections.Specialized;
using System.IO;
using CoyoEden.Models;

public partial class widgets_CategoryHotPosts_widget : WidgetBase
{
	protected void Page_Load(object sender, EventArgs e)
	{
		//css and js
		if (!Page.IsPostBack) {
			string jsPath = string.Format("{0}assets/js/widgets/{1}.js",Utils.RelativeWebRoot,this.Name);
			AddWidgetScript(jsPath);
		}
	}

	#region base implementation
	public override string Name
	{
		get { return "CategoryHotPosts"; }
	}

	public override bool IsEditable
	{
		get { return true; }
	}

	public override void LoadWidget()
	{
		LoadSettings();
		BuildLayout();
	}
	#endregion

	#region member variables
	private List<Category> Categories;
	private int PostsDisplayNumber=5;
	private int PostTitleSize=30;
	#endregion

	#region helper methods
	private void LoadSettings() {
		StringDictionary settings = GetSettings();
		if (settings.ContainsKey("clist"))
		{
			string tempStr = settings["clist"];
			string[] categoryIDs = tempStr.Split('|');
			Categories = new List<Category>();
			categoryIDs.ToList().ForEach(cID =>
			{
				var C = Category.GetCategory(Convert.ToInt32(cID));
				Categories.Add(C);
			});
		}
		if (settings.ContainsKey("showNum")) {
			PostsDisplayNumber = Convert.ToInt32(settings["showNum"]);
		}
		if (settings.ContainsKey("titleSize")) {
			PostTitleSize = Convert.ToInt32(settings["titleSize"]);
		}
	}
	private void BuildLayout() {
		if (Categories == null || Categories.Count == 0) {
			phWrapper.Visible = false;
			return;
		}
		HtmlAnchor anc = null;
		Categories.ForEach(c => {
			anc = new HtmlAnchor();
			anc.HRef = Utils.RelativeWebRoot + "category/" + c.Id + PortalSettings.Instance.FileExtension;
			anc.Title = "";
			anc.InnerHtml = c.Title;
			anc.Attributes["class"] ="";

			phChannelboxTitle.Controls.Add(anc);

			//build content
			BuildContentPart(c);
		});
		//clear float
		phChannelboxTitle.Controls.Add(new LiteralControl("<div class=\"clear\"></div>"));
	}
	private void BuildContentPart(Category c) {
		HtmlGenericControl dd = new HtmlGenericControl("dd");
		dd.Attributes["class"] = "cc";
		//content left part
		var posts = Post.GetPostsByCategory(c.Id, PortalSettings.Instance.IncludePostsOfChildren).OrderByDescending(item=>item.DateCreated).ToList();
		if (posts.Count > PostsDisplayNumber)
		{
			posts = posts.Take(PostsDisplayNumber).ToList();
		}

		PostTitleList leftpart = new PostTitleList();
		leftpart.CssClass = "channelpostlist left";
		leftpart.Posts = posts;
		leftpart.ForceRefresh = true;
		leftpart.TitleSize = PostTitleSize;
		dd.Controls.Add(leftpart);

		//content right part
		HtmlGenericControl rightpart = new HtmlGenericControl("div");
		rightpart.Attributes["class"] = "channeldescbox right";
		Image cIcon = new Image();
		cIcon.AlternateText = c.Title;
		cIcon.CssClass = "channelicon left";
		cIcon.ImageUrl = string.IsNullOrEmpty(c.Icon) ? "~/assets/img/unkown80x80.gif" : c.Icon;
		rightpart.Controls.Add(cIcon);
		rightpart.Controls.Add(new LiteralControl(c.Description));

		dd.Controls.Add(rightpart);

		//clear float div
		dd.Controls.Add(new LiteralControl("<div class=\"clear\"></div>"));

		phChannelboxContent.Controls.Add(dd);
	}
	#endregion
}
